<?php

/**
 * Created by JetBrains PhpStorm.
 * User: GMC
 * Date: 10/3/13
 * Time: 2:20 PM
 * To change this template use File | Settings | File Templates.
 */
class WidgetCategories extends CWidget {
    public $categories, $id, $child_ids;

    public function init() {
        $categories = Category::model()->findAll(
            array(
                'select' => 'cat_id,cat_name,products',
                'condition' => 'parent_id = 0',
                'order' => 't.order ASC'
            ));

        $parents = array();
        //get sub categories
        foreach ($categories as $category) {
            $content = '';
            $sub_categories = Category::model()->findAll(array(
                'select' => 'cat_id,cat_name,products',
                'condition' => 'parent_id = :id',
                'params' => array(':id' => $category->cat_id)
            ));
            foreach ($sub_categories as $item) {
                $content .= "<a href=" . TreTodayUrl::getCategoryUrl($item->cat_id, $item->cat_name) . ">» $item->cat_name</a>";
            }
            array_push($parents, array('parent' => $category->cat_name, 'sub' => $content));
        }

        $this->categories = $parents;
    }

    public function run() {
        return CWidget::render('widgetCategories', array('results' => $this->categories));
    }
}